package org.ruanjj.multithread;

public class Deposite {
    public static void main(String[] args) {
        Account account = new Account();
        Customer c1 = new Customer(account,"老王");
        Customer c2 = new Customer(account,"老李");

        c1.start();
        c2.start();
    }
}

class Customer extends Thread{
    private Account account;

    public Customer(Account a, String name){
        super(name);
        this.account = a;
    }

    @Override
    public void run(){
        for(int i = 0; i < 3; i++){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            account.excute(1000);
        }
    }

}


//账户
class Account{
    private int amount;

    public synchronized void excute(int amount){
        System.out.println(Thread.currentThread().getName()+"存入金额时，当前账户金额"+this.amount);
        this.amount = this.amount+amount;
        System.out.println(Thread.currentThread().getName()+"存入后，账户余额"+this.amount);
    }
}
